#include "Avatar.h"
#include "ltobjectcreate.h"
#include "serverinterfaces.h"
#include "windows.h"


const char* SocketID_Name[SocketID_Num] =
{
	"score"
};


Avatar::Avatar(HOBJECT &hObj)
{
	m_hObj = hObj;
	for(int i = 0;i<SocketID_Num;i++)
	{
		m_hAttachments[i] = 0;
		m_hAttachobjects[i] = 0;
	}
}

Avatar::~Avatar()
{
	m_hObj = 0;
}

bool Avatar::create_attachmentobject(HOBJECT &hObj, const char *sFilename, const char *sTexture)
{
	ObjectCreateStruct objCreateStruct;
	HCLASS hClass = g_pLTServer->GetClass("BaseClass");
	if(!hClass)
	{
		assert(!"Error:Couldn't get BaseClass!!");
		return FALSE;
	}
	objCreateStruct.Clear();
	objCreateStruct.m_ObjectType = OT_MODEL;
	objCreateStruct.m_Flags = FLAG_VISIBLE;

	strncpy(objCreateStruct.m_Filename,sFilename,127);
	strncpy(objCreateStruct.m_SkinNames[0],sTexture,127);

	BaseClass* m_pObj = (BaseClass*)g_pLTServer->CreateObject(hClass,&objCreateStruct);
	if(m_pObj)
	{
		hObj = m_pObj->m_hObject;
		g_pLTServer->CPrint("Attachment Object Created!");
		return TRUE;
	}
	return FALSE;	
}


bool Avatar::create_attachment(HOBJECT &hObj, HATTACHMENT &hAttachment, HOBJECT hChildObject, const char *sSocket, LTVector &vRotOffset, LTVector &vPosOffset)
{
	LTVector vOffset;
	LTRotation rOffset;

	vOffset.Init();
	rOffset.Init();
    hAttachment = NULL;

	HMODELSOCKET hSocket;
	LTRESULT SocketResult = g_pLTSModel->GetSocket(hObj,sSocket,hSocket);
	if(LT_OK == SocketResult)
	{
		g_pLTServer->CPrint("Got the socket");
	}
	else
	{
		g_pLTServer->CPrint("GetSocket failed");
		return FALSE;
	}
	rOffset.Rotate(rOffset.Right(),MATH_DEGREES_TO_RADIANS(vRotOffset.x));
	rOffset.Rotate(rOffset.Up(),MATH_DEGREES_TO_RADIANS(vRotOffset.y));
	rOffset.Rotate(rOffset.Forward(),MATH_DEGREES_TO_RADIANS(vRotOffset.z));

	LTRESULT resultAttachment = g_pLTServer->CreateAttachment(hObj,hChildObject,sSocket,&vOffset,&rOffset,&hAttachment);
	if(LT_OK != resultAttachment)
	{
		g_pLTServer->CPrint("Error creating attachment!!");
		return FALSE;
	}
	return TRUE;
}


bool Avatar::attach_object_to_socket(const char* sFilename,const char* sTexture,uint32 socketID,LTVector vRotOffset,LTVector vPosOffset)
{
	if(!m_hAttachments[socketID])
	{
		g_pLTServer->RemoveAttachment(m_hAttachments[socketID]);
		g_pLTServer->RemoveObject(m_hAttachobjects[socketID]);
	}

	if(!create_attachmentobject(m_hAttachobjects[socketID],sFilename,sTexture))
		return FALSE;
	if(!create_attachment(m_hObj,m_hAttachments[socketID],m_hAttachobjects[socketID],SocketID_Name[socketID],vRotOffset,vPosOffset))
		return FALSE;
    return  TRUE;
}

bool Avatar::deattach_object_to_socket(uint32 socketID)
{
	if(m_hAttachments[socketID])
	{
		g_pLTServer->RemoveAttachment(m_hAttachments[socketID]);
		g_pLTServer->RemoveObject(m_hAttachobjects[socketID]);
		return TRUE;
	}
	return FALSE;
}

bool Avatar::change_attach_object_to_socket(uint32 socketID, const char *sFilename, const char *sTexture,LTVector &vRotOffset, LTVector &vPosOffset)
{
	HOBJECT m_hOldAttachobject = m_hAttachobjects[socketID];
	HATTACHMENT m_hOldAttachment = m_hAttachments[socketID];
	if(!create_attachmentobject(m_hAttachobjects[socketID],sFilename,sTexture))
	{
		m_hAttachobjects[socketID] = m_hOldAttachobject;
		return FALSE;
	}
	if(!create_attachment(m_hObj,m_hAttachments[socketID],m_hAttachobjects[socketID],SocketID_Name[socketID],vRotOffset,vPosOffset))
	{
		m_hAttachments[socketID] = m_hOldAttachment;
		return FALSE;
	}
	return TRUE;
	
}